package com.normalClasses.eye;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class IpLocation {
	private String ip;
	private String city;
	public String get_city() {
		String host = "http://iploc.market.alicloudapi.com";
	    String path = "/v3/ip";
	    String method = "GET";
	    String appcode = "229aa18f4bd24852be4882bf2cc2c47d";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("ip", ip);
	    try {
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	//System.out.println(response.toString());
	    	//获取response的body直接对json进行拿值
//	    	System.out.println(EntityUtils.toString(response.getEntity()));
	      JSONObject msg=JSON.parseObject(EntityUtils.toString(response.getEntity()));
	      this.city=msg.getString("city");
//	      JSONObject res= msg.getJSONObject("showapi_res_body");
//	      JSONArray res1= res.getJSONArray("siteList");
//	      int index=0;
//	      for (Iterator<Object> iterator = res1.iterator(); iterator.hasNext(); ) {	
//	    	  JSONObject next = (JSONObject) iterator.next();
//	    	  System.err.println("pm2.5 ===>>> " + next.getString("pm2_5"));
//	    	  }   
//	
	      System.out.println(msg.getString("rectangle"));
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return 	this.city;
        }
public IpLocation(String ip) {
	this.ip=ip;
}
}
